#include <thread>
#include <vector>
#include <iostream>
#include <atomic>

std::atomic_flag lock = ATOMIC_FLAG_INIT;

void f(int n)
{
    while (lock.test_and_set(std::memory_order_acquire)) // 尝试获得锁
    {
        std::cout << "wait form thread " << n << std::endl;
    }
    std::cout << "thread " << n << " starts working" << std::endl;
}

void g(int n)
{
    std::cout << "thread " << n << " is going to start" << std::endl;
    lock.clear();
    std::cout << "thread " << n << " starts working" << std::endl;
}

int main()
{
    lock.test_and_set();
    std::thread t1(f, 1);
    std::thread t2(g, 2);

    t1.join();
    t2.join();
}
